<template>
  <MrCustom bg-color="bg-gradual-blue " is-back>
    <template #backText>
      返回
    </template>
  </MrCustom>
  <view v-if="appointmentInfo">
    <view class="bg-white text-center">
      <view class="padding  bg-gradual-orange radius"
            v-if="isBefore">
        <text class="text-black text-bold">距离开启时间</text>
        <view style="margin: 0 70px">
          <CountDown v-model:time="appointmentInfo.appointmentTime.split(',')[0]"></CountDown>
        </view>
      </view>
      <view v-else-if="isBetween">
        <view class="bg-gradual-green padding radius text-center shadow-blur">
          <view class="text-lg">已开启</view>
        </view>
      </view>
      <view v-else>
        <view class="bg-gradual-red padding radius text-center shadow-blur">
          <view class="text-lg">已结束</view>
        </view>
      </view>
    </view>
    <uni-group title="预约信息" mode="card">
      <uni-list>
        <uni-list-item title="预约编号" :rightText="appointmentInfo.id.toString()"/>
        <uni-list-item title="预约时间" :rightText="getStartEndTimeString(appointmentInfo.appointmentTime)"/>
        <uni-list-item title="预约类型" :rightText="getAppointmentType(appointmentInfo.type)"/>
        <uni-list-item title="创建时间" :rightText="appointmentInfo.createTime"/>
      </uni-list>
    </uni-group>
    <uni-group title="患者信息" mode="card">
      <uni-list>
        <uni-list-item title="患者用户名" :rightText="appointmentInfo.patientUsername"/>
        <uni-list-item title="患者姓名" :rightText="appointmentInfo.patientName"/>
      </uni-list>
    </uni-group>
    <uni-group title="医护信息" mode="card">
      <uni-list>
        <uni-list-item title="医护日程编号" :rightText="appointmentInfo.scheduleId.toString()"/>
        <uni-list-item title="医护用户名" :rightText="appointmentInfo.nurseUsername"/>
        <uni-list-item title="医护姓名" :rightText="appointmentInfo.nurseName"/>
      </uni-list>
    </uni-group>
    <view class="padding flex flex-direction">
      <button class="cu-btn bg-green shadow lg" @click="handleTreat">核 销</button>
    </view>

  </view>


</template>

<script setup lang="ts">
import {onReady} from "@dcloudio/uni-app";
import {getStartEndTimeString} from '@/utils/common/myUtils'
import {ref} from "vue";
import UniGroup from "@/uni_modules/uni-group/components/uni-group/uni-group.vue";
import {getAppointmentType} from '@/utils/common/myStandard'
import api from "@/api";
import myMoment from "@/utils/common/myMoment";
import CountDown from "@/components/common/CountDown.vue";
import MrCustom from "@/components/common/MrCustom.vue";


onReady(() => {
  scan()
})
const appointmentInfo = ref()
const isBefore = ref(false)
const isBetween = ref(false)

const scan = () => {
  uni.scanCode({
    success: function (res) {
      console.log('条码内容：' + res.result);
      api.appointment.id({
        id: res.result
      }).then(response => {
        if (response && response.data.code === 0) {
          if (response.data.data.status === 1) {
            appointmentInfo.value = response.data.data
            isBefore.value = myMoment().isBefore(appointmentInfo.value.appointmentTime.split(',')[0])
            isBetween.value = myMoment().isBetween(appointmentInfo.value.appointmentTime.split(',')[0], appointmentInfo.value.appointmentTime.split(',')[1])
          } else {
            uni.navigateBack({
              success: () => {
                uni.showToast({
                  title: '二维码无效',
                  icon: 'error'
                })
              }
            })
          }
        }
      })
    },
    fail: function (res) {
      uni.navigateBack({
        success: () => {
          uni.showToast({
            title: '二维码无效',
            icon: 'error',
          })
        }
      })

    }
  });
}
const handleTreat = () => {
  if (isBetween.value) {
    uni.showModal({
      title: '提示',
      content: '确定开启治疗',
      success: function (res) {
        if (res.confirm) {
          api.appointment.treat({
            id: appointmentInfo.value.id
          }).then(response => {
            if (response && response.data.code === 0) {
              appointmentInfo.value.status = 2
              uni.navigateTo({
                url: '/pages/nurse/appointment/handle/detail?appointmentInfo=' + encodeURIComponent(JSON.stringify(appointmentInfo.value))
              })
            }
          })
        } else if (res.cancel) {
        }
      }
    });
  } else {
    uni.showToast({
      title: '暂未开始',
      icon: 'error'
    })
  }
}
</script>

<style scoped>

</style>